package com.meitianhui.convenitenbuy.module.wallet.ui.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.meitianhui.convenitenbuy.App;
import com.meitianhui.convenitenbuy.Constants;
import com.meitianhui.convenitenbuy.R;
import com.meitianhui.convenitenbuy.base.BaseToolbarActivity;
import com.meitianhui.convenitenbuy.main.model.bean.EventBean;
import com.meitianhui.convenitenbuy.module.wallet.adapter.WalletCardRecordAdapter;
import com.meitianhui.convenitenbuy.module.wallet.contract.WalletCardRecordContract;
import com.meitianhui.convenitenbuy.module.wallet.di.DaggerWalletComponent;
import com.meitianhui.convenitenbuy.module.wallet.di.WalletModule;
import com.meitianhui.convenitenbuy.module.wallet.model.CardRecordBean;
import com.meitianhui.convenitenbuy.module.wallet.presenter.WalletCardRecordPresenter;
import com.meitianhui.convenitenbuy.util.RxBus;
import com.meitianhui.convenitenbuy.util.TipUtils;

import java.util.List;

import javax.inject.Inject;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

public class WalletCardRecordActivity extends BaseToolbarActivity implements WalletCardRecordContract.View, BaseQuickAdapter.RequestLoadMoreListener {
    RecyclerView mRv;
    LinearLayout mLlNone;
    private int pageNum = 1;
    @Inject
    WalletCardRecordPresenter mPresenter;
    private WalletCardRecordAdapter mAdapter;
    private String card_no;

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        DaggerWalletComponent.builder().appComponent(App.getInstance().getAppComponent()).walletModule(new WalletModule(this)).build().inject(this);
        card_no = getIntent().getStringExtra(Constants.ARGUMENT);
        initId();
        setTbTitle(getString(R.string.wallet_card_detail_record));
        initInfo();
        resisterPush();
    }

    private void resisterPush() {

        Disposable subscribe = RxBus.get().toFlowable(EventBean.class).subscribe(new Consumer<EventBean>() {
            @Override
            public void accept(EventBean bean) throws Exception {
                if (bean != null && bean.type == Constants.EVENT_TYPE_REFRESH) {
                    initInfo();
                }
            }
        });
        mCompositeDisposable.add(subscribe);
    }

    private void initInfo() {
        mPresenter.getTradeRecode(pageNum, card_no);
    }

    private void initId() {
        mRv = getViewById(R.id.rv_wallet_card_record);
        mLlNone = getViewById(R.id.ll_wallet_card_record_none);
        LinearLayoutManager mLinearManager = new LinearLayoutManager(this);
        mLinearManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRv.setLayoutManager(mLinearManager);
    }

    @Override
    protected int setView() {
        return R.layout.activity_wallet_card_record;
    }

    @Override
    public void showError(String msg) {

    }

    @Override
    protected void onRefreshData() {
        if (mAdapter.isLoading()) mAdapter.loadMoreEnd();
        pageNum = 1;
        mPresenter.getTradeRecode(pageNum, card_no);
    }

    @Override
    public void onLoadMoreRequested() {
        pageNum++;
        initInfo();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mAdapter != null && mAdapter.isLoading()) mAdapter.loadMoreEnd();
        refreshFinish();
    }

    @Override
    public void getTradeRecodeBack(boolean result, List<CardRecordBean> list, String msg) {
        if (result) {
            if (mAdapter == null) {
                mAdapter = new WalletCardRecordAdapter(this, list);
                mRv.setAdapter(mAdapter);
                mAdapter.setOnLoadMoreListener(this, mRv);
            } else {
                if (mAdapter.isLoading()) mAdapter.loadMoreComplete();
                if (pageNum == 1) {
                    mAdapter.replaceData(list);
                } else {
                    mAdapter.addData(list);
                }
            }
            if (mAdapter.isLoading()) mAdapter.loadMoreComplete();
            mAdapter.setEnableLoadMore(list.size() < 20 ? false : true);
            mLlNone.setVisibility(list.isEmpty() && pageNum == 1 ? View.VISIBLE : View.GONE);
        } else {
            TipUtils.showToast(App.getInstance(), msg);
            mLlNone.setVisibility(View.VISIBLE);
        }
        refreshFinish();
    }
}
